package com.scoutmobmap.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.scoutmobmap.shared.GreetRequest;
import com.scoutmobmap.shared.GreetResponse;
import com.scoutmobmap.shared.GreetingService;
import com.scoutmobmap.shared.OfferDto;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
    GreetingService {

  public GreetResponse greetServer(GreetRequest req) throws IllegalArgumentException {
    GreetResponse toReturn = new GreetResponse();
    ObjectifyService.register(Offer.class);
    ObjectifyService.register(OfferList.class);
    Objectify ofy = ObjectifyService.begin();
    
    toReturn.offers = new ArrayList<OfferDto>();
    Iterable<Key<Offer>> allKeys = ofy.query(Offer.class).fetchKeys();
    Map<Key<Offer>, Offer> fetched = ofy.get(allKeys);
    for (Offer o : fetched.values()) { toReturn.offers.add(o.toDto()); }

    return toReturn;
  }
}
